﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace JMR.Components
{
    public class Screenshot
    {
        private Form _parent = null;
        private Form _frame = null;

        private bool _coordinatesInitialized = false;
        private int _left = 0;
        private int _top = 0;
        private int _width = 0;
        private int _height = 0;

        public Screenshot(Form parent)
        {
            _parent = parent;
            _coordinatesInitialized = false;
        }

        public Bitmap TakeScreenshot(int left, int top, int width, int height)
        {
            Bitmap resultBMP = null;

            _coordinatesInitialized = true;
            _left = left;
            _top = top;
            _width = width;
            _height = height;

            resultBMP = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            Graphics gfxScreenshot = Graphics.FromImage(resultBMP);
            gfxScreenshot.CopyFromScreen(left, top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

            return resultBMP;
        }

        public Bitmap TakeScreenshot()
        {
            Bitmap resultBMP = null;

            if (_coordinatesInitialized == true)
            {
                resultBMP = this.TakeScreenshot(_left, _top, _width, _height);
            }

            return resultBMP;
        }

        public Bitmap TakeScreenshot(bool keepWindowOpenAfterShot)
        {
            Bitmap resultBMP = null;

            if (_frame == null) return resultBMP;

            _frame.Visible = false;

            resultBMP = this.TakeScreenshot(_frame.Left, _frame.Top, _frame.Width, _frame.Height);

            _frame.Visible = keepWindowOpenAfterShot;
            if (_frame.Visible == false)
            {
                _frame = null;
            }

            return resultBMP;
        }

        public void showFrame()
        {
            if (_frame == null)
            {
                _frame = new Photoframe();
                _frame.Show(_parent);
            }
        }

    }
}
